package cn.jinjimi.app.modules.Resource;

import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.entity.AppConfig.AppModule;
import cn.jinjimi.app.managers.ConfigManager;
import cn.jinjimi.app.modules.IModule;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roy
 * Date: 2017/3/3
 */

public class Resource implements IModule<Resource.Config> {

    private static volatile Resource mInstance = null;
    private Config mConfig;

    public static Resource getInstance() {
        if (mInstance == null) {
            synchronized (Resource.class) {
                if (mInstance == null) {
                    mInstance = new Resource();
                }
            }
        }
        return mInstance;
    }

    @Override
    public Config getConfig() {
        if (mConfig == null) {
            String data = ConfigManager.getInstance().getConfig(AppModule.ModuleResource);
            mConfig = GsonUtils.fromJson(data, Config.class);
        }
        return mConfig;
    }

    public Config createConfig() {
        String data = ConfigManager.getInstance().getConfig(AppModule.ModuleResource);
        mConfig = GsonUtils.fromJson(data, Config.class);
        return mConfig;
    }


    private List<Bundle> mBundles;

    public List<Bundle> getBundle() {
        return mBundles == null ? createBundle() : mBundles;
    }

    /**
     * type=0 :web resource bundle
     * type=1 :react-native patch bundle
     * @param type
     * @return
     */
    public List<Bundle> getBundlesOfType(int type){
        List<Bundle> bundles = new ArrayList<>();

        for(Bundle b:getBundle()){
            if(b.type == type){
                bundles.add(b);
            }
        }

        return bundles;
    }

    public List<Bundle> createBundle() {
        String data = ConfigManager.getInstance().getConfig(AppModule.ModuleBundle);
        mBundles = GsonUtils.fromJson(data, new TypeToken<List<Bundle>>(){}.getType());
        return mBundles;
    }

    public static class Config {

        /* 静态资源版本号 */
        @SerializedName("version")
        public String version;

        /* 静态资源MD5码 */
        @SerializedName("md5")
        public String md5;

        /* 静态资源下载地址*/
        @SerializedName("link")
        public String link;

        @SerializedName("id")
        public String id;

        @SerializedName("notify_url")
        public String notifyUrl;

    }

    public static class Bundle {
        /**
         * Bundle类型
         */
        public int type;

        /**
         * Bundle体积
         */
        public int size;

        @SerializedName("entrance")
        public String entranceUrl;

        @SerializedName("md5")
        public String md5;

        @SerializedName("link")
        public String link;

        @SerializedName("id")
        public String id;

        @SerializedName("notify_url")
        public String notifyUrl;
        /**
         * 合并后的bundle摘要
         */
        @SerializedName("bundle_md5")
        public String bundleMd5;

        public int height;

        public String mainComponentName = "APP";

        public String getMainComponentName(){
            return mainComponentName;
        }

        public boolean isValid() {
            return StringUtils.isNotEmpty(entranceUrl)
                    && StringUtils.isNotEmpty(md5)
                    && StringUtils.isNotEmpty(link)
                    && StringUtils.isNotEmpty(id);
        }
    }

}
